//
//  Pop_Cat_View.m
//  MindBox
//
//  Created by MS on 16-1-2.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "Pop_Cat_View.h"
#import "ItemBaseView.h"
#import "UIView+Frame.h"
@interface Pop_Cat_View ()
@property (nonatomic,strong) NSMutableArray * popArray;

@end

@implementation Pop_Cat_View
{
    UILabel * titleView;
}

-(void)setPop_cat:(NSArray *)pop_cat
{
    _pop_cat = pop_cat;
    titleView.text = @"主题馆";
    
    [_popArray removeAllObjects];
    
    [self addItemBaseViewWithCount:pop_cat.count];
    
    for (int i =0; i<pop_cat.count; i++) {
        Pop_Cat * popcat = pop_cat[i];
        ItemBaseView * baseView = self.popArray[i];
        baseView.popCat = popcat;
    }
}


-(void)addItemBaseViewWithCount:(NSInteger)count
{
    for (int i=0; i<count; i++) {
        ItemBaseView * baseView = [ItemBaseView itemBaseView];
        baseView.backgroundColor =[UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
        baseView.tag =i;
        [self addSubview:baseView];
        [self.popArray addObject:baseView];
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)];
        [baseView addGestureRecognizer:tap];
    }

}

-(NSMutableArray *)popArray
{
    if (_popArray==nil) {
        _popArray= [NSMutableArray array];
    }
    return _popArray;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor =[UIColor whiteColor];
        
        titleView = [[UILabel alloc] init];
        titleView.font = [UIFont systemFontOfSize:12];
        [self addSubview:titleView];
    }
    return self;
}

-(void)tapView:(UITapGestureRecognizer *)tap
{
    NSInteger index =tap.view.tag;
    
    if ([self.delegate respondsToSelector:@selector(pop_cat_ViewDidClick:pop_cat:index:)]) {
        [self.delegate pop_cat_ViewDidClick:self pop_cat:self.pop_cat[index] index:index];
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    NSInteger count = self.popArray.count;
    CGFloat margin = 10;
    CGFloat w =(self.width-30)/2;
    CGFloat h = (self.height - CGRectGetMaxY(titleView.frame)-(count)*margin)/(count*0.5);
//    CGFloat h =60;
    int i=0;
    titleView.frame = CGRectMake(10, 10, self.width, 20);
    for (ItemBaseView * baseView in self.popArray) {
        
        int col =i%2;
        int rol =i/2;
        CGFloat x = margin + (margin+w)*col;
        CGFloat y = CGRectGetMaxY(titleView.frame)+margin +(margin+h)*rol;
        baseView.frame = CGRectMake(x, y, w, h);
        i++;
    }
    
}

@end
